<template>
  <div id="app">
    <!-- <router-view/> -->
    <router-view v-if="isRouterAlive" />
  </div>
</template>
<script>
import { setHtmlFontSize } from "@/common";
import {
  getRouter,
  getAddRoutes,
  blankPage,
} from "@/common/conversion/index.js";

export default {
  name: "App",
  //页面刷新数据回显
  provide() {
    return {
      reload: this.reload,
    };
  },
  data() {
    return {
      isRouterAlive: true,
    };
  },
  methods: {
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(function () {
        this.isRouterAlive = true;
      });
    },
    resizeHtmlFontSize() {
      return setHtmlFontSize();
    },
    // 取到缓存路由,进行转换并添加实现刷新不丢失路由
    async refreshSave() {
      let routes = [];
      await getRouter().then((res) => {
        routes = res;
      });
      if (routes && routes.length > 0) {
        await blankPage(routes, 0);
        await getAddRoutes(routes);
        this.$router.addRoutes(routes);
      }
    },
  },
  mounted() {
    // 路由转换
    this.refreshSave();

    setHtmlFontSize();

    window.addEventListener("resize", this.resizeHtmlFontSize);
    this.$once("hook:beforeDestroy", () => {
      window.removeEventListener("resize", this.resizeHtmlFontSize);
    });
  },
};
</script>

<style lang="scss">
html,
body,
#app {
  width: 100%;
  height: 100%;
}
</style>